package com.sj.stone.controller;


import com.sj.stone.domain.mysql.ShipInfoRepository;
import com.sj.stone.domain.mysql.StoreRepository;
import com.sj.stone.domain.mysql.UserRepository;
import com.sj.stone.domain.mysql.entry.ShipInfo;
import com.sj.stone.domain.mysql.entry.Store;
import com.sj.stone.domain.mysql.entry.User;
import com.sj.stone.domain.oracle.*;
import com.sj.stone.domain.oracle.entry.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "/member")
public class MemberController {
    @Autowired
    private RegistDataRepository registDataRepository;
    @Autowired
    private MemberRepository memberRepository;
    @Autowired
    private RegionRepository regionRepository;
    @Autowired
    private StoreRepository storeRepository;
    @Autowired
    private BlackListRepository blackListRepository;
    @Autowired
    private AdminRepository adminRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private ShipInfoRepository shipInfoRepository;

    public static final String ZFADMIN = "42dd4cfccd9343f0804f75bd8b62e1a0";

    //查找客户信息
    @RequestMapping("/findMember")
    @ResponseBody
    public String findMember(String phone) {
        String cbbc = "";

        Member member = memberRepository.findByMobile(phone.trim());
        if (member == null) {
            cbbc = "未查到该店铺";
        } else {
            Admin admin = adminRepository.findOne(member.getAdminid());
            Region reare = regionRepository.findOne(member.getArea());
            Region rcity = regionRepository.findOne(member.getCity());
            Region rtown = regionRepository.findOne(member.getTown());
            cbbc = "店铺名称:" + member.getUsername() + ",店主名:" + member.getTruename() + ",地址:" + member.getAddress()+
                    ",店铺状态:"+member.getDisabled() + ",负责业务:" +admin.getTruename() +  ",所在城市:" +
                     rcity.getName() +",所在区域:" + reare.getName() + ",所在乡镇:" + rtown.getName() +
                    ",注册时间:" + member.getRegtime();
        }

        return cbbc;

    }

    //查看关联信息
    @RequestMapping("/lookRegist")
    @ResponseBody
    public String lookRegist(String appPhone) {
        String cbbc = "";
        RegistData registData = registDataRepository.findByLoginAccount(appPhone.trim());
        if (registData == null) {
            cbbc = "当前手机号未关联";
        } else {
            cbbc = "店铺名称:" + registData.getShopName() + ",店主名:" + registData.getConsignee() + ",关联地址:" +
                    registData.getReceivingAddress() + ",关联时间:" + registData.getCreatetime() +
                    ",注册区域ID:" + registData.getRegionid() + ",关联商城店铺ID:" + registData.getMemberId();
        }


        return cbbc;
    }

    //注销客户
    @RequestMapping("/deleteMember")
    @ResponseBody
    public String deleteMember(String mobile) {

        Member member = memberRepository.findByMobile(mobile.trim());
        member.setDisabled("true");
        memberRepository.save(member);
        return "已注销成功";
    }

    //区域关闭货到付款
    @RequestMapping("/closeDf")
    @ResponseBody
    public String closeDf(String district) {
        String cbbc = "该区域货到付款已关闭";
        BlackList bl = new BlackList();
        List<Region> rglist = regionRepository.findByName(district.trim());
        if (rglist.size() == 1) {
            List<Member> memberList = memberRepository.findByArea(rglist.get(0).getId());
            for (int n = 0; n < memberList.size(); n++) {
                memberList.get(n).setAdminid(ZFADMIN);
                memberRepository.save(memberList.get(n));
                bl.setId(memberList.get(n).getId());
                blackListRepository.save(bl);
            }
            List<Store> storeList = storeRepository.findByDistrict(rglist.get(0).getName());
            for (int m = 0; m < storeList.size(); m++) {
                storeList.get(m).setServiceby(ZFADMIN);
                storeRepository.save(storeList.get(m));
            }
        } else {
            for (int i = 0; i < rglist.size(); i++) {
                if (rglist.get(i).getGrade() == 3) {
                    List<Member> mlist = memberRepository.findByArea(rglist.get(i).getId());
                    for (int n = 0; n < mlist.size(); n++) {
                        mlist.get(n).setAdminid(ZFADMIN);
                        memberRepository.save(mlist.get(n));
                        bl.setId(mlist.get(n).getId());
                        blackListRepository.save(bl);
                    }
                    List<Store> storeList = storeRepository.findByDistrict(rglist.get(i).getName());
                    for (int m = 0; m < storeList.size(); m++) {
                        storeList.get(m).setServiceby(ZFADMIN);
                        storeRepository.save(storeList.get(m));
                    }
                    break;
                } else {
                    cbbc = "未找到该区域";
                }
            }
        }

        return cbbc;
    }

    //区域开通货到付款
    @RequestMapping("/openDf")
    @ResponseBody
    public String openDf(String district, String userName) {
        String cbbc = "该区域货到付款已开通,负责业务:";
        List<Region> rglist = regionRepository.findByName(district.trim());
        Admin admin = null;
        try {
            admin = adminRepository.findAllByUsername(userName.trim());
        } catch (Exception e) {
            cbbc = "用户名不唯一";
            e.printStackTrace();
        }
        if (rglist.size() == 1) {
            List<Member> memberList = memberRepository.findByArea(rglist.get(0).getId());
            for (int n = 0; n < memberList.size(); n++) {
                memberList.get(n).setAdminid(admin.getId());
                memberRepository.save(memberList.get(n));
                BlackList blackList = blackListRepository.findOne(memberList.get(n).getId());
                if (blackList != null) {
                    blackListRepository.delete(blackList.getId());
                }
            }
            List<Store> storeList = storeRepository.findByDistrict(rglist.get(0).getName());
            for (int m = 0; m < storeList.size(); m++) {
                storeList.get(m).setServiceby(admin.getId());
                storeRepository.save(storeList.get(m));
            }
        } else {
            for (int i = 0; i < rglist.size(); i++) {
                if (rglist.get(i).getGrade() == 3) {
                    List<Member> memberList = memberRepository.findByArea(rglist.get(i).getId());
                    for (int n = 0; n < memberList.size(); n++) {
                        memberList.get(n).setAdminid(admin.getId());
                        memberRepository.save(memberList.get(n));
                        BlackList blackList = blackListRepository.findOne(memberList.get(n).getId());
                        if (blackList != null) {
                            blackListRepository.delete(blackList.getId());
                        }
                    }
                    List<Store> storeList = storeRepository.findByDistrict(rglist.get(i).getName());
                    for (int m = 0; m < storeList.size(); m++) {
                        storeList.get(m).setServiceby(admin.getId());
                        storeRepository.save(storeList.get(m));
                    }
                    break;
                } else {
                    cbbc = "未找到该区域";
                }
            }
        }

        return cbbc + admin.getTruename();
    }

    //单个客户关闭货到付款
    @RequestMapping("/closeOneDf")
    @ResponseBody
    public String closeOneDf(String mobile) {

        String cbbc = "已关闭";
        Member member = null;
        User user = null;
        try {
            member = memberRepository.findByMobile(mobile.trim());
            user = userRepository.findByMobile(mobile.trim());
        } catch (Exception e) {
            cbbc = "用户手机号不唯一";
            e.printStackTrace();
        }
        BlackList blackList = new BlackList();
        blackList.setId(member.getId());
        blackListRepository.save(blackList);
        member.setAdminid(ZFADMIN);
        memberRepository.save(member);
        Store store = storeRepository.findByUserid(user.getId());
        store.setServiceby(ZFADMIN);
        storeRepository.save(store);

        return cbbc;
    }

    //单个客户开通货到付款
    @RequestMapping("/openOneDf")
    @ResponseBody
    public String openOneDf(String mobile, String userName) {
        String cbbc = "已开通";
        Admin admin = null;
        Member member = null;
        User user = null;
        try {
            admin = adminRepository.findAllByUsername(userName.trim());
            member = memberRepository.findByMobile(mobile.trim());
            user = userRepository.findByMobile(mobile.trim());
        } catch (Exception e) {
            cbbc = "业务用户名或客户手机号不唯一";
            e.printStackTrace();
        }
        member.setAdminid(admin.getId());
        memberRepository.save(member);
        blackListRepository.delete(member.getId());
        Store store = storeRepository.findByUserid(user.getId());
        store.setServiceby(member.getId());
        storeRepository.save(store);
        return cbbc;
    }

    @RequestMapping("/updateMemberAddress")
    @ResponseBody
    public String updateMemberAddress(String mobile, String address) {
        String cbbc = "修改成功";
        Member member = null;
        User user = null;
        try {
            member = memberRepository.findByMobile(mobile.trim());
            user = userRepository.findByMobile(mobile.trim());
        } catch (Exception e) {
            cbbc = "手机号不唯一";
            e.printStackTrace();
        }
        member.setAddress(address.trim());
        memberRepository.save(member);
        Store store = storeRepository.findByUserid(user.getId());
        store.setAddress(address.trim());
        storeRepository.save(store);
        ShipInfo shipInfo = shipInfoRepository.findByUserid(user.getId());
        shipInfo.setAddress(address.trim());
        shipInfoRepository.save(shipInfo);

        return cbbc;
    }
}
